package com.alks.function.controller.stockdept.pcfactorystockquery;

import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.stockdept.pcfactorystockwork.*;
import com.alks.function.service.impl.stockdept.pcfactorystockquery.MyOrdDto;
import com.alks.function.service.impl.stockdept.pcfactorystockquery.MyOrdPrintDto;
import com.alks.function.service.stockdept.pcfactorystockquery.IPcFitDegService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@ApiRestController("/pcFactorStockQuery")
@Api(value = "仓储部查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
@Slf4j
@RoleCheck(prgIds = {"160110"})
public class PcFitDegControlller {
    @Autowired
    IPcFitDegService service;

    @PutMapping("/getFdRqsQuery")
    @ApiOperation(value = "贴合委外加工-请购查询-分页可条件查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getFdRqsQuery(@RequestBody FdRqsRequest request) {
        return service.getFdRqsQuery(request);
    }

    @PutMapping("/fdPurDisAdd")
    @ApiOperation(value = "贴合委外加工-下单批处理-批量新增", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo fdPurDisAdd(@RequestBody List<PurDisAddRequest> request) {
        return service.fdPurDisAdd(request);
    }

    @PutMapping("/getOrdQuery")
    @ApiOperation(value = "贴合委外加工-(我的/全部)订购单-可条件查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getOrdQuery(@RequestBody OrdQueryRequest request) {
        Map<String, Object> query = service.getOrdQuery(request);
        return ResponseInfo.ok(query);
    }


    @PostMapping("/getFdLotDetQuery")
    @ApiOperation(value = "贴合委外加工-指令明细-条件查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getFdLotDetQuery(@RequestParam @ApiParam("请购单号") String reqNo,
                                         @RequestParam @ApiParam("指令") String purLot,
                                         @RequestParam @ApiParam("物料编号") String mtlNo) {
        return service.getFdLotDetQuery(reqNo, purLot, mtlNo);
    }

    @PostMapping("/getFdMtlSumQuery")
    @ApiOperation(value = "贴合委外加工-贴合子材料-条件查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getFdMtlSumQuery(@RequestParam @ApiParam("请购单号(出库批处理只要这个)") String reqNo,
                                         @RequestParam @ApiParam("指令") String purLot,
                                         @RequestParam @ApiParam("物料编号") String mtlNo) {
        return service.getFdMtlSumQuery(reqNo, purLot, mtlNo);
    }

    @PutMapping("/fdMtlSumUpdate")
    @ApiOperation(value = "贴合委外加工-出库批处理更新-更新", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo fdMtlSumUpdate(@RequestBody List<MtlSumUpRequest> request,
                                       @RequestParam(required = false) @ApiParam("更新标志 1为更新 2为出库批处理") Integer flag) {
        return service.fdMtlSumUpdate(request, flag);
    }

    @PostMapping("/fdMtlSumCancel")
    @ApiOperation(value = "贴合委外加工-出库单取消-更新", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo fdMtlSumCancel(@RequestParam(required = false) @ApiParam("出库单号") String slipNo) {
        return service.fdMtlSumCancel(slipNo);
    }

    @PutMapping("/myOrdUpdate")
    @ApiOperation(value = "贴合委外加工-我的订单修改-更新", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo myOrdUpdate(@RequestBody MyOrdUpRequest request) {
        return service.myOrdUpdate(request);
    }

    @PostMapping("/myOrdDelete")
    @ApiOperation(value = "贴合委外加工-我的订单修改-更新", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo myOrdDelete(@RequestParam Integer id) {
//        return service.myOrdDelete(id);
        return ResponseInfo.ok();
    }

    @PutMapping("/creatPurUpdate")
    @ApiOperation(value = "贴合委外加工-形成订购单-批量更新", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo creatPurUpdate(@RequestBody List<MyOrdUpRequest> request) {
        return service.creatPurUpdate(request);
    }


    @GetMapping("/getFitReamarkQuery")
    @ApiOperation(value = "贴合委外加工-贴合备注-查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getFitReamarkQuery() {
        return service.getFitReamarkQuery();
    }

    @GetMapping("/getPurBack")
    @ApiOperation(value = "贴合委外加工-退单", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getPurBack(@RequestParam Integer id) {
        service.getPurBack(id);
        return ResponseInfo.ok();
    }

    @PostMapping("/getOrdPrint")
    @ApiOperation(value = "贴合委外加工-我的订单打印-查询", tags = "仓储部-仓储工厂作业-贴合委外加工")
    public ResponseInfo getOrdPrint(@RequestParam @ApiParam(value = "1.选中订单 2.未打印订单", required = true) Integer type,
                                    @RequestBody(required = false) List<String> purNoList) {
        /*构件入参*/
        List<MyOrdDto> dtoList;
        if (type.equals(2)) {
            OrdQueryRequest request = new OrdQueryRequest();
            request.setFlag2(1);
            request.setFlag1(2);
            request.setPageNum(-1);
            request.setPageSize(-1);
            /*获取数据*/
            Map<String, Object> query = service.getOrdQuery(request);
            /*数据处理*/
            dtoList = (List<MyOrdDto>) query.get("dtos");
        } else if (type.equals(1)) {
            if (ArrayUtils.isEmpyt(purNoList)) {
                System.out.println("请先生成订单号");
            }
            dtoList = service.getExportByPurNo(purNoList);
        } else {
            return ResponseInfo.error("参数错误！");
        }
        if (ArrayUtils.isEmpyt(dtoList)) {
            return ResponseInfo.error("暂无数据");
        }
        Map<String, List<MyOrdDto>> map = dtoList.stream()
                .sorted(Comparator.comparing((MyOrdDto dto) -> Optional.ofNullable(dto.getInDate()).orElse(LocalDate.of(9999, 12, 31).atStartOfDay())))
                .collect(Collectors.groupingBy(MyOrdDto::getPurNo));
        List<MyOrdPrintDto> printDtoList = new ArrayList<>();
        map.forEach((k, v) -> {
            MyOrdPrintDto printDto = new MyOrdPrintDto();
            printDto.setPurNo(k);
            printDto.setInDate(v.get(0).getVendorDate());
            printDto.setVendorName(v.get(0).getVendorName());
            printDto.setStkName("原材料仓");
            printDto.setSysDate(LocalDateTime.now());
            printDto.setSysName(UserIdThread.get().getUserName());
            printDto.setCompanyId(UserIdThread.get().getCompanyId());
            printDto.setMyOrdList(v);
            printDtoList.add(printDto);
        });
        return ResponseInfo.ok(printDtoList);
    }
}
